package com.choosefine.it.company.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.IdsUtil;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.company.model.CompanyPublishproject;
import com.choosefine.it.company.model.dto.SelectCompanyPublishByConditionDTO;
import com.choosefine.it.company.model.dto.UpdateCompanyFindManagerVerifyStatusDTO;
import com.choosefine.it.company.model.vo.CompanyPublishManagerVo;
import com.choosefine.it.company.service.CompanyPublishprojectService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(value="建筑公司-招项目承包人",description="陈健")
@RequestMapping("/companyPublishproject")
public class CompanyPublishprojectController extends BaseController {
	Logger logger = LoggerFactory.getLogger("建筑公司招项目承包人");
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	ObjectMapper jsonObjectMapper;
	@Autowired
	CompanyPublishprojectService companyPublishprojectService;
	
	@ApiOperation("建筑公司-发布项目")
	@RequestMapping(value="/saveCompanyPublishproject",method=RequestMethod.POST,produces="application/json")
	public String saveCompanyPublishproject(@RequestBody CompanyPublishproject companyPublishproject ) throws JsonProcessingException{
		String result=null;
		if(StringUtils.isNotBlank(companyPublishproject.getUserCode())){
			Integer isSuccess = companyPublishprojectService.saveCompanyPublishproject(companyPublishproject);
			if(isSuccess.intValue()>0){
				result= responseSuccess(isSuccess, ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
			}else{
				result= responseFail(ResultCode.FAILED,ResultCode.MSG_ERROR);
			}
		}else{
				result= responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	@ApiOperation("建筑公司-修改招项目承包人信息")
	@RequestMapping(value="/updateCompanyPublishproject",method=RequestMethod.PUT)
	public String updateCompanyPublishproject(@RequestBody CompanyPublishproject companyPublishproject ){
		String result=null;
		companyPublishproject.setUtime(DateUtils.getNowTime());
		companyPublishproject.setPublishTime(DateUtils.getNowTime());
		companyPublishproject.setPublisher(null);
		companyPublishproject.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		companyPublishproject.setReason(null);
		Integer isSuccess = companyPublishprojectService.updateCompanyPublishinfo(companyPublishproject);
		if(isSuccess.intValue()>0){
			result= responseSuccess(isSuccess, ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
			return result;
		}else{
			result= responseFail(ResultCode.FAILED,ResultCode.MSG_PARAMS_NULL);
			return result;
		}
	}
	/**
	 * 曾文铸
	 * 根据id修改建筑公司招项目经理信息的状态
	 * @param publishstatus
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据id修改建筑公司招项目经理信息的发布状态", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "publishStatus", value = "通过0不通过1", required = true, dataType = "Integer",paramType = "query"),
						 @ApiImplicitParam(name = "id", value = "该条记录的id", required = true, dataType = "Integer", paramType = "query")})
	@RequestMapping(method=RequestMethod.PUT,value="/updatePublishstatus",produces="application/json")
	public String updateFindProjectInfoPublishstatusByID(@RequestParam Integer publishStatus,
			@RequestParam Integer id){
		String result= null;
		if(publishStatus!=null&&id!=null){
			Map<String, Object> params = new HashMap<>();
			params.put("publishStatus", publishStatus);//发布状态（通过0不通过1）
			params.put("id", id);
			params.put("utime", DateUtils.getNowTime());
			int num=companyPublishprojectService.updateFindProjectInfoPublishstatusByID(params);   //执行CompanyFindManagerMapper里面的接口
			if(num>0){
				result = responseSuccess(num);
			}else{
				result = responseFail();
			}
		}else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	@ApiIgnore
	@RequestMapping(value = "/selectCompanyPublishByCondition", method = RequestMethod.POST)
	public String selectFindProjectInfoByCondition(@RequestBody SelectCompanyPublishByConditionDTO dto) throws UnsupportedEncodingException {
		String result = null;
		PageDTO page = new PageDTO();
		page.setPageNum(dto.getPageNum());
		page.setPageSize(dto.getPageSize());
		Map<String, Object> params = new HashMap<>();
		if (!StringUtils.isEmpty(dto.getProjectName())) {
			params.put("projectName", dto.getProjectName());
		}
		if (StringUtils.isNotBlank(dto.getUserCode())) {
			params.put("userCode", dto.getUserCode());
		}
		if (StringUtils.isNotBlank(dto.getProjectTypeName())){
			params.put("projectTypeName", dto.getProjectTypeName());
		}
		if (dto.getStartTime() != null 
				&& null != dto.getEndTime()) {
			params.put("startTime", dto.getStartTime());
			params.put("endTime", dto.getEndTime());
		}
		if(dto.getVerifyStatus() != null){
			params.put("verifyStatus", dto.getVerifyStatus());
		}
		Page<CompanyPublishManagerVo> list = companyPublishprojectService
				.selectCompanyFindManagerByCondition(params, page);
		result = responseSuccess(list.toPageInfo());
		return result;
	}
	
	@ApiIgnore
	@RequestMapping(value = "/updateCompanyFindManagerVerifyStatus", method = RequestMethod.PUT)
	public String updateManagerFindProjectVerifyStatus(@RequestBody UpdateCompanyFindManagerVerifyStatusDTO dto) {
		String result = null;
		Map<String, Object> params = new HashMap<>();
		if (dto.getId() != null
				&& !StringUtils.isEmpty(dto.getPublisher())
				&& dto.getVerifyStatus() != null
				&& (dto.getVerifyStatus().equals(0)||dto.getVerifyStatus().equals(1)||dto.getVerifyStatus().equals(2))) {
			params.put("id", dto.getId());
			params.put("verifyStatus", dto.getVerifyStatus());
			params.put("publisher", dto.getPublisher());
			params.put("utime", DateUtils.getNowTime());
			if (!StringUtils.isEmpty(dto.getReason())){
				params.put("reason", dto.getReason());
			}
			int isSuccess = companyPublishprojectService.updateVerifyStatus(params);
			if (isSuccess > 0) {
				result = responseSuccess(isSuccess,
						ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_UPDATE_FAILD);
			}
		} else {
			responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}
	
	@ApiIgnore
	@RequestMapping(value = "/deleteFindProject/{idss}", method = RequestMethod.GET)
	public String deleteManagerFindProject(@PathVariable String idss){
		List<Integer> ids = new ArrayList<>();
		String result = null;
		Map<String, Object> params = new HashMap<>();
		ids=IdsUtil.getIds(idss);
		if (ids.size() > 0) {
			params.put("ids", ids);
			params.put("utime", DateUtils.getNowTime());
			int isSuccess = companyPublishprojectService.deleteFindManager(params);
			if (isSuccess == ids.size()) {
				result = responseSuccess(isSuccess,
						ResultCode.MSG_UPDATE_SUCCESS);
			} else if (0 < isSuccess && isSuccess < ids.size()) {
				result = responseSuccess(isSuccess,
						ResultCode.MSG_UPDATEPART_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_UPDATE_FAILD);
			}
		} else {
			responseFail(ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}
}
